; docformat = 'rst'
;
;+
;
; :Purpose:
;   Run CELS framework
;   
; :Inputs:
;   Sim_name: Simulation name that can be specified, otherwise sim_name variable can be written in code
;   
; :Keywords:
;   Mozart: (Input) Run MOZART-specific routines
;   Spinup: (Input) Run CELS framework including pre-Start year measurements and sensitivities
;   Metropolis: (Input) Run Metropolis algorithm
;   Invert_only: (Input) Set to TRUE to skip sensitivity calculation, and just do inversion
;
; :Outputs:
;   Many files output.  Inversion results in optimized.sav
;   
; :Example::
;   IDL> CELS_RUN, 'SF6', /mozart, /spinup, /metropolis
;
; :History:
; 	Written by: Matt Rigby, MIT, Aug 19, 2011
;
;-
pro cels_run, sim_name, mozart=mozart, spinUp=spinUp, metropolis=metropolis, invert_only=invert_only

  compile_opt idl2
  
  if invert_only eq 0 then begin

    cels_measurements, sim_name
    if keyword_set(spinup) then cels_measurements, sim_name, /spinUp

    cels_define_regions, sim_name
    cels_emissions_lpdm, sim_name
    cels_define_state, sim_name
    cels_aggregate, sim_name, /do_plot
    
    if keyword_set(mozart) then begin
      cels_mozart_emissions, sim_name
      cels_mozart_preproc, sim_name
      cels_mozart_strong_oxidant, sim_name
      cels_mozart_sensitivity, sim_name
      if keyword_set(spinup) then cels_mozart_sensitivity_spinup, sim_name
    endif  

    cels_sensitivity, sim_name

  endif else begin

    cels_invert, sim_name

    if keyword_set(metropolis) then begin
      cels_countries_to_track, sim_name
      cels_invert_metropolis, sim_name
    endif

  endelse

end